package DB;

import java.sql.ResultSet;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

/**
 *
 * @author Mariano
 */
public class BDManejo {
    
    public ResultSet runQuery(String sentence){
        ResultSet res = null;
        try {
            Statement sent = (Statement) DBConeccion.getInstance().createStatement();
            res = sent.executeQuery(sentence);
        }
        catch (Exception ex){
            ex.printStackTrace();
        } 
        return res;
    }

    public void runUpdate(String sentence){
        try {
            Statement stm = (Statement) DBConeccion.getInstance().createStatement();
            stm.executeUpdate(sentence);
        }
        catch  (Exception ex){
            ex.printStackTrace();
        }	
    }

    public PreparedStatement newRecord(String table, String[] columns){
        PreparedStatement stmt = null;
        try {
            String values = "";
            String columnNames = "";
            int i = 0;
            while (i != columns.length){
                values += "?,";
                columnNames += (columnNames.equals(""))? columns[i] : ", " + columns[i];
                i++;
            }
            values = (String) values.subSequence(0, values.length()-1);

            String command = "INSERT INTO " + table + " (" + columnNames + ") VALUES (" + values +")";
            stmt = (PreparedStatement) DBConeccion.getInstance().prepareStatement(command);
        }
        catch (Exception ex){
            System.err.println("ERROR createInsertStatement");
            ex.printStackTrace();
        }
        return stmt;
    }

    public void create(java.sql.PreparedStatement stm) {
        try {
            stm.executeUpdate();
        }
        catch  (Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void update(String tableName, String column, String value, String id2, String id){
    	String query = "UPDATE "+ DBConeccion.bd+"."+tableName;
    	query += " set " + column+ " = '"+value+"' where "+ id2 +" = '"+id+"';";
        runUpdate(query);
    }

    public void delete(String tableName, String columnName, String id){
    	String query = "DELETE from " + DBConeccion.bd + "." + tableName; 
    	query += " Where " + columnName + " = \"" + id +"\";";
    	runUpdate(query);
    }
    
    public ResultSet showAll(String tableName){
     	ResultSet result = null;
    	String query = "SELECT * from " + DBConeccion.bd + "." + tableName + ";";
    	result = runQuery( query );
    	return result;
    }

    public ResultSet show(String tableName, String id, String id2){
     	ResultSet result = null;
    	String query = "SELECT * from " + DBConeccion.bd + "." + tableName + " WHERE "+id+"=" + id2 + ";";
    	result = runQuery( query );
    	return result;
    }

    public ResultSet getByField(String tableName, String fieldName, String value){
    	ResultSet result = null;
    	String query = "SELECT * from " + DBConeccion.bd + "." + tableName + " WHERE " + fieldName + "= \"" + value + "\";";
    	result = runQuery( query );
    	return result;
    }
 
    public ResultSet listar(String tableName,String columns, String fieldName, String value){
    	ResultSet result = null;
    	String query = "SELECT "+ columns +" from " + DBConeccion.bd + "." + tableName + " WHERE " + fieldName + "= \"" + value + "\";";
    	result = runQuery( query );
    	return result;
    }
}